<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // 1.数组去重
        let fruits = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"]

        // 第一种方法 Array.from()  实用扩展运算符...
        let unique_fruit = Array.from(new Set(fruits))
        console.log(unique_fruit)  // ["banana", "apple", "orange", "watermelon", "grape"]

        // 第二种方法 扩展运算符
        let unique_fruit2 = [...new Set(fruits)] // ["banana", "apple", "orange", "watermelon", "grape"]
        console.log(unique_fruit2)

        // 2.替换数组中的特定值
        let fruits_1 = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"]
        fruits_1.splice(0, 2, 'enen', 'haha') // ["enen", "haha", "orange", "watermelon", "apple", "orange", "grape", "apple"]
        console.log(fruits_1)

        // 3.不使用.map()映射数组
        let friends = [
            { name: 'John', age: 22 },
            { name: 'Peter', age: 23 },
            { name: 'Mark', age: 24 },
            { name: 'Maria', age: 22 },
            { name: 'Monica', age: 21 },
            { name: 'Martha', age: 19 },
        ]
        let friendNames = Array.from(friends, ({ name }) => name)
        console.log(friendNames) // ["John", "Peter", "Mark", "Maria", "Monica", "Martha"]

        // 4.清空数组
        let fruits_2 = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"]
        fruits_2.length = 0
        console.log(fruits_2) // []

        // 5.数组转对象
        let fruits_3 = ["banana", "apple", "orange", "watermelon"];
        let fruits_3_obj = { ...fruits_3 }
        console.log(fruits_3_obj) // {0: "banana", 1: "apple", 2: "orange", 3: "watermelon"}

        // 6.用数据填充数组
        let newArray = new Array(10).fill('1')
        console.log(newArray) // ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1"]

        // 7.合并数组
        var fruits_4 = ['apple', 'banana”', 'orange”'];
        var meat = ['poultry”', 'beef”', 'fish”'];
        var vegetables = ['potato', 'tomato', 'cucumber'];
        var food = [...fruits_4, ...meat, ...vegetables];
        console.log(food) //  ["apple", "banana”", "orange”", "poultry”", "beef”", "fish”", "potato", "tomato", "cucumber"]

        // 8.求交集
        var numOne = [0, 2, 4, 6, 8, 8];
        var numTwo = [1, 2, 3, 4, 5, 6];

        // var bbb = new Set(numOne)
        // console.log(bbb, typeof bbb) // {0, 2, 4, 6, 8} "object"
        // var ccc = [...bbb]
        // console.log(ccc) // [0, 2, 4, 6, 8]

        // 先去重，后扩展
        var duplicatedValues = [...new Set(numOne)].filter(item => {
            return numTwo.includes(item)
        })
        // var duplicatedValues = [...new Set(numOne)].filter(item => numTwo.includes(item)) // 也可以这么写的

        console.log(duplicatedValues) // [2, 4, 6]

        // 9.从数组中删除虚值
        var mixedArr = [0, 'blue', '', NaN, 9, true, undefined, 'white', false]
        var trueArr = mixedArr.filter(Boolean)
        console.log(trueArr)  // ["blue", 9, true, "white"]

        // 10.从数组中获取随机值
        var colors = ['blue', 'white', 'green', 'navy', 'pink', 'purple', 'orange', 'yellow', 'black', 'brown']
        var colors_random = colors[(Math.floor(Math.random() * (colors.length)))]
        console.log(colors_random) // 随机打印数组中的任何一个值

        // 11.反转数组
        let fruits_5 = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"]
        let fruits_5_arr = fruits_5.reverse()
        console.log(fruits_5) // ["apple", "grape", "orange", "apple", "watermelon", "orange", "apple", "banana"]

        // 12.lastIndexOf 方法
        var nums = [1, 5, 2, 6, 3, 5, 2, 3, 6, 5, 2, 7]
        var lastIndexOf = nums.lastIndexOf(5)
        console.log(lastIndexOf) // 9

        // 对数组中的所有值求和
        var nums_sum = [1, 5, 2, 6]
        var nums_sum_result = nums_sum.reduce((x,y) => x + y )
        console.log(nums_sum_result) // 14
    </script>
</head>

<body>

</body>

</html>